{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  徐子诚\n",
    "+ 学号  0165215"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入副歌循环几遍  4\n",
      "主歌部分\n",
      "副歌部分\n",
      "副歌部分\n",
      "副歌部分\n",
      "副歌部分\n"
     ]
    }
   ],
   "source": [
    "def repeat_verse(verse,num):\n",
    "    nums=int(num)\n",
    "    for i in range (nums):\n",
    "        print(verse)\n",
    "        \n",
    "verse_1='主歌部分'\n",
    "verse_2='副歌部分'\n",
    "nums=input('请输入副歌循环几遍  ')\n",
    "\n",
    "print(verse_1)\n",
    "repeat_verse(verse_2,nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入数字个数是  1\n",
      "请输入一个数字1\n",
      "\n",
      "均值为1.0\n",
      "\n",
      "\n",
      "方差为1.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def mean_value(arrays):\n",
    "    sum=0\n",
    "    for num in arrays:\n",
    "        nums=int(num)\n",
    "        sum=sum+nums\n",
    "    sum=sum/len(arrays)\n",
    "    return sum\n",
    "    \n",
    "def variance(arrays):\n",
    "    sum=0\n",
    "    for num in arrays:\n",
    "        nums=int(num)\n",
    "        sum=sum+(nums-mean_value(arrays))*(nums-mean_value(arrays))\n",
    "    sum=sum/len(arrays)\n",
    "    return sum\n",
    "    \n",
    "    \n",
    "length=input(\"输入数字个数是  \")\n",
    "arrays=[]\n",
    "for x in range(int(length)):\n",
    "    xx=input('请输入一个数字')\n",
    "    arrays.append(xx)\n",
    "print('\\n均值为%s\\n'%mean_value(arrays))\n",
    "print('\\n方差为%s\\n'%mean_value(arrays))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入数字个数是  1\n",
      "请输入一个数字1\n",
      "请输入 1-求均值 \t 2-求方差 \t 0-退出0\n"
     ]
    }
   ],
   "source": [
    "def choose_s(arrays):\n",
    "    while True:\n",
    "        num=input('请输入 1-求均值 \\t 2-求方差 \\t 0-退出')\n",
    "        if num=='1':\n",
    "            print('\\n均值为%s\\n'%mean_value(arrays))\n",
    "        elif num=='2':\n",
    "            print('\\n方差为%s\\n'%mean_value(arrays))\n",
    "        else:\n",
    "            break\n",
    "            \n",
    "length=input(\"输入数字个数是  \")\n",
    "arrays=[]\n",
    "for x in range(int(length)):\n",
    "    xx=input('请输入一个数字')\n",
    "    arrays.append(xx)            \n",
    "\n",
    "choose_s(arrays)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def choose_sta(arrays,define):\n",
    "    \n",
    "    if define=='1':\n",
    "        print('最大值为%s\\t最小值为%s\\n'%(max(arrays),min(arrays)))\n",
    "\n",
    "    while True:\n",
    "        num=input('请输入 1-求均值 \\t 2-求方差 \\t 0-退出')\n",
    "        if num=='1':\n",
    "            print('\\n均值为%s\\n'%mean_value(arrays))\n",
    "        elif num=='2':\n",
    "            print('\\n方差为%s\\n'%mean_value(arrays))\n",
    "        else:\n",
    "            break\n",
    "            \n",
    "length=input(\"输入数字个数是  \")\n",
    "arrays=[]\n",
    "for x in range(int(length)):\n",
    "    xx=input('请输入一个数字')\n",
    "    arrays.append(xx)            \n",
    "\n",
    "define=input('是否输出最大值最小值？\\n（输入1为是）')\n",
    "choose_sta(arrays,define)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
